/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.list;

import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.TableElement;
import com.openwaf.client.event.EventManager;
import com.openwaf.client.event.logical.SelectionChangeEvent;
import com.openwaf.client.event.logical.SelectionChangeHandler;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewElement;
import java.util.ArrayList;

public abstract class AbstractList extends UIControl{
    protected ArrayList elements;
    @ViewElement
    protected TableElement tblRoot;
    protected static String CSS_NORMAL="waf-list-item";
    protected static String CSS_SELECTED="waf-list-item-selected";
    protected EventManager eventmanager;
    protected AbstractList(){
        elements=new ArrayList();        
    }
    public void addSelectionChangeHandler(SelectionChangeHandler handler){
        if(eventmanager==null){
            eventmanager=new EventManager();
        }
        eventmanager.addEventHandler(SelectionChangeEvent.NAME, handler);
    }
    public  void clear(){
        elements.clear();
        while(tblRoot.getRows().length>0){
            tblRoot.deleteRow(0);
        }
    }
    public abstract void addElement(Element elm);
    public abstract void addElement(UIControl elm);
    public abstract void addElement(String elm);
    public abstract void addElement(int index,Element elm);
    public abstract void addElement(int index,UIControl elm);
    public abstract void addElement(int index,String elm);
    public abstract boolean isSelected(int index);
    public abstract void select(int index);
    public abstract void deselect(int index);
    public ArrayList getSelectedItems(){
        int l=elements.size();
        ArrayList al=new ArrayList();
        for(int i=0;i<l;i++){
            if(isSelected(i)){
                al.add(elements.get(i));
            }
        }
        return al;
    }
    public int getSelectedItemCount(){
        int l=elements.size();
        int count=0;
        for(int i=0;i<l;i++){
            if(isSelected(i)){
                count++;
            }
        }
        return count;
    }
     public void clearSelection(){
        int l=elements.size();
        for(int i=0;i<l;i++){
            deselect(i);
        }
    }
    public void selectAll(){
        int l=elements.size();
        for(int i=0;i<l;i++){
            select(i);
        }
    }
        
    public int getElementCount(){
        return elements.size();
    }

    public Object removeElementAt(int index){
        checkIndex(index);
        return elements.remove(index);
    }
    public boolean removeElement(Object obj){
        return elements.remove(obj);
    }
    protected void checkIndex(int index){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index out of bounds index:"+index+" size:"+elements.size());
        }
    }
    public Object getElementAt(int index){
        checkIndex(index);
        return elements.get(index);
    }
    public int indexOf(Object obj){
        return elements.indexOf(obj);
    }
    public void setBorderWidth(int width){
        tblRoot.setBorder(width);
    }
}
